package com.ciwong.epaper.modules.epaper.adapter;


import android.content.Context;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;

import com.ciwong.epaper.R;
import com.ciwong.epaper.modules.bookstore.bean.PublishWorkContent;
import com.ciwong.epaper.modules.epaper.ui.TeaCatalogActivity;
import com.ciwong.epaper.modules.epaper.ui.TeaWorkContentActivity;

import java.util.List;

/**
 * 选择作业内容适配器
 */
public class ChooseWorkContentAdapter extends BaseAdapter {
    //作业内容集合
    private List<PublishWorkContent> workNameList;

    private int FROM_CHOOSE = 1;
    private Context mContext;
    private TextView choose_work_name_text;
    private ImageView remove_work_img;

    public ChooseWorkContentAdapter(List<PublishWorkContent> workNameList,Context mContext){
        this.workNameList = workNameList;
        this.mContext = mContext;
    }

    public void setNewData(List<PublishWorkContent> workNameList){
        this.workNameList = workNameList;
        this.notifyDataSetChanged();
    }

    @Override
    public int getCount() {
        return workNameList.size();
    }

    @Override
    public Object getItem(int i) {
        return workNameList.get(i);
    }

    @Override
    public long getItemId(int i) {
        return i;
    }

    @Override
    public View getView(final int i, View view, ViewGroup viewGroup) {
        if (null == view){
            view = View.inflate(mContext, R.layout.activity_choose_work_content_item,null);
        }
        choose_work_name_text = view.findViewById(R.id.choose_work_name_text);
        remove_work_img = view.findViewById(R.id.remove_work_img);
        choose_work_name_text.setText(workNameList.get(i).getResourceName());
        remove_work_img.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                ChooseWorkContentAdapter.this.notifyDataSetChanged();
                if (mContext instanceof TeaWorkContentActivity){
                    ((TeaWorkContentActivity)mContext).setWorkCountAndTimeConsuming(FROM_CHOOSE,workNameList.get(i).getVersionId(),workNameList.get(i).getResourceUrl(),workNameList.get(i).getResourceName());
                }

                if (mContext instanceof TeaCatalogActivity){
                    ((TeaCatalogActivity)mContext).setWorkCountAndTimeConsuming(FROM_CHOOSE,workNameList.get(i).getVersionId(),workNameList.get(i).getResourceUrl(),workNameList.get(i).getResourceName());
                }
                //workNameList.remove(i);
            }
        });
        return view;
    }
}
